#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10

void findmin(int juzhen[MAXSIZE][MAXSIZE],int n,int *minr,int *minc,int *secondminr,int *secondminc)
{
	int min=juzhen[0][0];
	int secondmin=juzhen[0][0];
	*minr=0;
	*minc=0;
	*secondminr=0;
	*secondminc=0;
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(juzhen[i][j]>juzhen[0][0])
			{
				secondmin=juzhen[i][j];
				*secondminr=i;
				*secondminc=j;
				break;
			}
		}
		if(secondmin>juzhen[0][0])
		{
			break;
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(juzhen[i][j]<min)
			{
				secondmin=min;
				*secondminr=*minr;
				*secondminc=*minc;
				min=juzhen[i][j];
				*minr=i;
				*minc=j;
			}
			else if(juzhen[i][j]<secondmin&&juzhen[i][j]!=min)
			{
				secondmin=juzhen[i][j];
				*secondminr=i;
				*secondminc=j;
			}
		}
	}
} 
void swap(int *a,int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
}
int main()
{
	int n;
	int juzhen[MAXSIZE][MAXSIZE];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			scanf("%d",&juzhen[i][j]);
		}
	}
	int minr,minc,secondminr,secondminc;
	findmin(juzhen,n,&minr,&minc,&secondminr,&secondminc);
	swap(&juzhen[0][0],&juzhen[minr][minc]);
	swap(&juzhen[n-1][n-1],&juzhen[secondminr][secondminc]);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(j!=n-1)
			{
				printf("%d ",juzhen[i][j]);
			}
			else
			{
				printf("%d",juzhen[i][j]);
			}
		}
		printf("\n");
	}
	return 0;
}
